package com.king.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.{DataFrame, SparkSession}

/**
 * @Author wdl
 * @Date 2022/12/1 16:22
 */
object SparkSQL02_UDF {

  def main(args: Array[String]): Unit = {

    val sparkConf: SparkConf = new SparkConf().setAppName("SparkSQL").setMaster("local[*]")

    val spark: SparkSession =
      SparkSession.builder()
        //.appName("SparkSQL")
        //.master("local[*]")
        .config(sparkConf)
        .getOrCreate()

    //写代码开始不管用不用都导入。
    import spark.implicits._

    val df: DataFrame = spark.read.json("E:\\work\\big-data-2020\\spark-api\\input\\user.json")
    df.createOrReplaceTempView("user")

    //需求: 对name列进行处理,需要在列的前面添加前缀.
    //注册函数
    spark.udf.register("prefixName", (name: String) => "prefix : " + name)

    spark.sql("select id ,prefixName( name ) ,age from user").show()

    spark.stop()

  }
}
